package zdream.rockchronicle.screen.menu;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import zdream.control.assets.TextureDescription;
import zdream.control.assets.TextureDescription.Region;
import zdream.control.world.Ticker;
import zdream.rockchronicle.RockChronicle;
import zdream.control.graphics.IPainter;

import static zdream.rockchronicle.screen.menu.GameMenu.*;

/**
 * 绘制 LevelScreen 里面的菜单栏的
 * 
 * @author Zdream
 * @date 2021-08-22 (last modified)
 */
public class GameMenuPainter implements IPainter {
	
	final GameMenu gameMenu;
	public OrthographicCamera symbolCamera;

	@Override
	public boolean hasLoaded() {
		return panelBg != null;
	}

	@Override
	public void load() {
		panelBg = new Texture(Gdx.files.local("res/misc/menu_panel.png"));
	}

	/**
	 * 背景图片
	 */
	Texture panelBg;
	
	/**
	 * MISC
	 */
	TextureDescription textures;
	BitmapFont font;
	SpriteBatch batch;

	TextureRegion pauseTextureRegion, cursorTextureRegion, keyMappingTextureRegion;

	public GameMenuPainter(GameMenu gameMenu) {
		this.gameMenu = gameMenu;
		this.batch = gameMenu.app.graphics.uiBatch;
	}

	void init() {
		textures = gameMenu.app.assets.getDescription("res/misc/menu_misc_sheet.json");
		font = gameMenu.app.graphics.font24;
	}

	void prepare() {
		if (pauseTextureRegion == null) {
			pauseTextureRegion = gameMenu.app.assets.findTextureRegion(textures.findRegion("title-暂停中"));
		}
		if (cursorTextureRegion == null) {
			cursorTextureRegion = gameMenu.app.assets.findTextureRegion(textures.findRegion("cursor"));
		}
		if (keyMappingTextureRegion == null) {
			keyMappingTextureRegion = gameMenu.app.assets.findTextureRegion(textures.findRegion("title-键位设置"));
		}
	}

	@Override
	public void draw(Ticker ticker) {
		if (gameMenu.status != STATUS_OPEN) {
			return;
		}

		prepare();
		batch.begin();
		batch.draw(panelBg, 0, 0);

		try {
			switch (gameMenu.getPosition()) {
			case POSITION_MAIN:
				drawMain(batch);
				break;
			case POSITION_KEY_SETTING:
				drawKeySetting(batch);
				break;

			default:
				break;
			}
		} finally {
			batch.end();
		}
	}

	private void drawMain(SpriteBatch batch) {
		final RockChronicle app = gameMenu.app;
		SpriteBatch uiBatch = app.graphics.uiBatch;
		Region entry = textures.findRegion("title-暂停中");
		batch.draw(pauseTextureRegion, app.width / 2f - (entry.width / 2f), 548, entry.width, entry.height);
		
		String[] texts = GameMenu.MainMenuItem.TEXTS;
		GameMenu.MenuItem item = gameMenu.items.items[0];
		
		for (int i = 0; i < texts.length; i++) {
			String text = texts[i];
			int x = app.width / 2 - 45;
			int y = 380 - 60 * i;
			
			font.setColor(Color.BLACK);
			font.draw(uiBatch, text, x + 2, y - 2);
			font.setColor(Color.WHITE);
			font.draw(uiBatch, text, x, y);
			
			if (item.cursor == i) {
				entry = textures.findRegion("cursor");
				batch.draw(cursorTextureRegion, x - 90, y - 24, entry.width, entry.height);
			}
		}
	}
	
	private void drawKeySetting(SpriteBatch batch) {
		final RockChronicle app = gameMenu.app;
		SpriteBatch uiBatch = app.graphics.uiBatch;
		Region entry = textures.findRegion("title-键位设置");
		batch.draw(keyMappingTextureRegion, app.width / 2f - (entry.width / 2f), 548, entry.width, entry.height);
		
		String[] texts = GameMenu.KeySettingItem.TEXTS;
		GameMenu.KeySettingItem item = (GameMenu.KeySettingItem) gameMenu.items.items[0];
		int startIndex = Math.max(0, Math.min(item.cursor - 7, item.cursorMax - 14));
		int endIndex = startIndex + 15;
		
		// 最多显示 15 行
		for (int i = startIndex; i < endIndex; i++) {
			String text = texts[i];
			int x = 220;
			int y = 500 - 30 * (i - startIndex);
			
			font.setColor(Color.BLACK);
			font.draw(uiBatch, text, x + 2, y - 2);
			font.setColor(Color.WHITE);
			font.draw(uiBatch, text, x, y);
			
			String[] keys = item.queryBindingKeys(text);
			if (keys != null) {
				for (int j = 0; j < keys.length; j++) {
					int textX;
					switch (j) {
					case 0: textX = 590; break;
					case 1: textX = 730; break;
					case 2: textX = 870; break;
					default: continue;
					}
					font.setColor(Color.BLACK);
					font.draw(uiBatch, keys[j], textX + 2, y - 2);
					font.setColor(Color.WHITE);
					font.draw(uiBatch, keys[j], textX, y);
				}
			}
			
			if (item.cursor == i) {
				entry = textures.findRegion("cursor");
				batch.draw(cursorTextureRegion, x - 40, y - 24, entry.width, entry.height);

				int cursorX;
				switch (item.cursorHorizontal) {
				case 1: cursorX = 560; break;
				case 2: cursorX = 700; break;
				case 3: cursorX = 840; break;
				default: continue;
				}
				batch.draw(cursorTextureRegion, cursorX, y - 24, entry.width, entry.height);
			}
		}
		
	}

	@Override
	public int zIndex() {
		return 100;
	}

	@Override
	public void dispose() {
		// do-nothing
	}
}
